﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Aspose.Cells;
using HCMessage.Model;

namespace HCBusiness
{
    public class EnvironmentCategory
    {

        public List<EnvCategory> GetEnvironmentGategoryList()
        {
            var env = new HCData.EnvironmentCategory();
            return env.GetEnvironmentGategoryList();
        }

        public bool UpdateItemInfo(EnvEntity message)
        {
            var workbook = new Workbook(message.ItemInputFile.InputStream);
            var itemList = new List<EnvEntity>();
            if (!string.IsNullOrEmpty(message.ItemName))
            {
                itemList.Add(message);
            }
            for (int i = 0; i < workbook.Worksheets.Count; i++)
            {
                var envCode = workbook.Worksheets[i].Name;
                for (int j = 1; j < workbook.Worksheets[i].Cells.MaxDataRow + 1; j++)
                {
                    var item = new EnvEntity();
                    item.ItemEnvironment = envCode;
                    item.ItemName = workbook.Worksheets[i].Cells[j, 0].StringValue.Trim();
                    item.ItemAddress = workbook.Worksheets[i].Cells[j, 1].StringValue.Trim();
                    itemList.Add(item);
                }
            }
            var env = new HCData.EnvironmentCategory();
            foreach (var r in itemList)
            {
                r.ItemId = env.UpdateItemName(r.ItemName);
                env.UpdateItemDetailName(r);
            }

            return true;
        }

        public ItemReport GetItemList()
        {
            var result = new ItemReport();
            var env = new HCData.EnvironmentCategory();
            result.EnvCtList = env.GetEnvironmentGategoryList();
            result.ItemReportList = env.GetItemReportDetail();
            return result;
        }
    }
}
